<?php
namespace Admin\Controller;

use Admin\Model\MessageModel;

class MessageController extends AdminController {
    
    public function index() {
        $title = I('title');
        $map='';
        if($title){
            $map['title'] = array('like','%'.$title.'%');
        }
        $map['type'] =0;
        $list = $this->lists('message', $map, 'id DESC');
        $this->assign('_list',$list);
        $this->meta_title = '消息列表';
        $this->display('list');
    }
    
    public function add() {
        $Message = D('message');
        if (IS_POST) {
            $data = $Message->create();
            $data || $this->error($Message->getError());

            $res = $Message->add();
            $list = M('ucenter_member')->where('id !=1')->select();
            foreach ($list as $li){
                $add_data['uid'] =  $li['id'];
                $add_data['msg_id'] =  $res;
                $add_data['uptime'] = time();
                M('message_user')->add($add_data);
            }
            //$title =I('title');
          //  $flag=  R('Push/pushOrder',array($title,''));

            $res !== false ? $this->success('新增成功', U('index')) : $this->error('未知错误');
        }

        $this->meta_title = '新增消息';
        $this->display('add');
    }
    
    public function view() {
        $Message = D('message');
        $id = I('get.id', 0);
        $info = $Message->find($id);
        $types = array(
            MessageModel::TYPE_ORDER => MessageModel::TYPE_ORDER_TEXT,
            MessageModel::TYPE_NOTICE => MessageModel::TYPE_NOTICE_TEXT,
            MessageModel::TYPE_EVENT => MessageModel::TYPE_EVENT_TEXT
        );
        $info['type_text'] = $types[$info['type']];
        $this->assign('info', $info);
        $this->meta_title = '查看消息';
        $this->display('view');
    }
    
    public function del() {
        $id = array_unique((array)I('id',0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        
        $map = array('id' => array('in', $id) );
        if(M('message')->where($map)->delete()){
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }
    
}